package sgd.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import sgd.db.oracle.DBConnector;
import sgd.db.table.TableAutore;

public class Library extends JFrame{

	private static final long serialVersionUID = 1L;

	private JLabel lb_error;

	private JTextField tf_autore_nome;
	private JTextField tf_autore_cogn;
	private JTextField tf_autore_anno;

	private JButton bt_autore_insert;
	private JButton bt_autore_select;

	private JTextArea ta_autore;

	private DBConnector dbc;

	public Library(){
		super("Library Database Connector");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(640, 480);

		dbc = new DBConnector("cbonesan", "oraclei3a");

		getContentPane().setLayout(new BorderLayout());

		// mask for new author
		lb_error = new JLabel();

		tf_autore_nome = new JTextField("");
		tf_autore_cogn = new JTextField("");
		tf_autore_anno = new JTextField("");

		JPanel j1 = new JPanel(new GridLayout(3,4));
		j1.add(new JLabel("Insert Author"));
		j1.add(new JLabel("First Name"));
		j1.add(tf_autore_nome);
		j1.add(new JLabel());
		j1.add(new JLabel("Last Name"));
		j1.add(tf_autore_cogn);
		j1.add(lb_error);
		j1.add(new JLabel("Birth Year"));
		j1.add(tf_autore_anno);

		// button panel
		bt_autore_insert = new JButton("Insert this autore");
		bt_autore_select = new JButton("Select all inserted authors");

		JPanel j2 = new JPanel(new FlowLayout());
		j2.add(bt_autore_insert);
		j2.add(bt_autore_select);

		// result set area
		ta_autore = new JTextArea();

		JPanel j3 = new JPanel(new FlowLayout());
		j3.add(ta_autore);

		getContentPane().add(j1,BorderLayout.NORTH);
		getContentPane().add(j2,BorderLayout.CENTER);
		getContentPane().add(j3,BorderLayout.SOUTH);

		setVisible(true);

		// action listener

		bt_autore_insert.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				lb_error.setForeground(Color.red);
				TableAutore a;
				if (tf_autore_nome.getText().equals("")){
					lb_error.setText("The first name is not valid!");
					return;
				}
				if (tf_autore_cogn.getText().equals("")){
					lb_error.setText("The last name is not valid!");
					return;
				}

				if (tf_autore_anno.getText().equals(""))
					a = new TableAutore(tf_autore_nome.getText(), tf_autore_cogn.getText());
				else
					try {
						Integer n = Integer.parseInt(tf_autore_anno.getText());
						a = new TableAutore(tf_autore_nome.getText(), tf_autore_cogn.getText(), n);						
					} catch (Exception e) {
						lb_error.setText("Year is not an integer!");
						return;
					}
				String fields = TableAutore.getFields3();
				if (a.getAnnoNascita() == 0)
					fields = TableAutore.getFields2();
					
				boolean flag = dbc.insert(fields, TableAutore.getTable(), a.getValue());
				if (flag) {
					lb_error.setForeground(Color.green);
					lb_error.setText("Insert completed");
				} else {
					lb_error.setForeground(Color.red);
					lb_error.setText("Insert failed");
				}
			}
		});

		bt_autore_select.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ta_autore.setText("");
				ArrayList<String> strs = dbc.select(TableAutore.getFields3(), TableAutore.getTable());
				if (strs.isEmpty()){
					lb_error.setForeground(Color.red);
					lb_error.setText("No data received!");
					return;
				}

				Iterator<String> it = strs.iterator();
				while(it.hasNext())
					ta_autore.append(it.next());

				lb_error.setForeground(Color.green);
				lb_error.setText("Selection complete.");
			}
		});

	}

}
